
FPrate <- function(org, res) {
    ret <- 0
    n <- dim(org)[2]

    for (i in 1:dim(org)[2]) {
        temp = sum((org[,i] - res[,i]) < 0) / sum(org[,i] == 0)

        if (is.na(temp) | temp == 0) {
            n = n-1
        }
        else {
            ret = ret + temp
        }
    }
    return(ret/n)
}

TPrate <- function(org, res) {
    ret <- 0
    n <- dim(org)[2]

    for (i in 1:dim(org)[2]) {
        temp =  sum((org[,i] + res[,i]) == 2) / sum(org[,i] == 1)

        if (is.na(temp) | temp == 0) {
            n = n-1
        }
        else {
            ret = ret + temp
        }
    }
    return(ret/n)
}

precision <- function(org, res) {
    ret <- 0
    n <- dim(org)[2]

    for (i in 1:dim(org)[2]) {
        temp = sum((org[,i] + res[,i]) == 2) / sum(res[,i] == 1)

        if (is.na(temp) | temp == 0) {
            n = n-1
        }
        else {
            ret = ret + temp
        }
    }
    return(ret/n)
}

TA <- function(org, res) {
    ret <- 0
    for (i in 1:dim(org)[2]) {
        ret = ret + (sum(org[,i] == res[,i]) / dim(org)[1])
    }
    return(ret/dim(org)[2])
}

sensitivity <- function(org, res) {
    return(TPrate(org, res))
}

specificity <- function(org, res) {
    return(1 - FPrate(org, res))
}

Fmeasure <- function(org, res) {
    p <- precision(org, res)
    r <- TPrate(org, res)

    return((2*p*r)/(p+r))
}

